'use strict';
app.controller('goodsAuditCtrl',['$scope','utilService','goodsAuditService','toaster','$modal',
    function($scope,utilService,goodsAuditService,toaster,$modal){
    // call server.
    $scope.goods = {
      status:'2',
      checkType:1
    };
    //查询待审核商品
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'goods/audit',
            params:$scope.goods
        }).then(function(result){
        });
    }
    $scope.query();

    //审核通过或拒绝
    $scope.changeStatus= function(id,stutas){
        $scope.data = {
            id:id,
            status:stutas
        };
        goodsAuditService.changeStatus($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //批量审核通过
    $scope.batchUpdateUpStatus=function(stutas){
        $scope.data = {
            status:stutas
        };
        goodsAuditService.batchUpdateUpStatus($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.types=result.data;
                    $scope.query();
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    //批量选择
    $scope.selected=[];

    $scope.isChecked = function(item){
        return $scope.selected.indexOf(item) >= 0 ;
    } ;

    $scope.updateSelection = function($event,item){

        var checkbox = $event.target ;
        var checked = checkbox.checked ;

        if(checked){
            $scope.selected.push(item) ;
        }else{
            var idx = $scope.selected.indexOf(item) ;
            $scope.selected.splice(idx,1) ;
        }
    };

    $scope.isCheckedAll=function(){

        if($scope.list==null){
            return;
        }

        return $scope.selected.length==$scope.list.length;
    }

    $scope.checkAll=function($event){
        var checkbox = $event.target ;
        var checked = checkbox.checked ;

        for(var i=0;i<$scope.list.length;i++){
            if(checked){
                $scope.selected.push($scope.list[i]);
            }else{
                $scope.selected.splice(0,$scope.selected.length);
            }
        }
    }

    $scope.initSelect=function(){
        $scope.selected=[];
    }

    //打开查看服务框
    $scope.checkReserve = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/goods/reserve_info.html',
            controller: 'reserveInfoCtrl',
            size: 'md',
            scope:$scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };

}]);

app.controller('reserveInfoCtrl',['$scope','toaster','data','$modalInstance','goodsAuditService',
    function($scope,toaster,data,$modalInstance,goodsAuditService){
    $scope.id=data;
    $scope.goodsInfo={};

    $scope.checkReserve=function(){
        goodsAuditService.checkReserve($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goodsInfo=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.checkReserve();

    $scope.ok = function(){
        $modalInstance.dismiss('cancel');
    };

}]);

//查询商品分类下拉框controller
app.controller('selectCtrl',['$scope','goodsAuditService','toaster', function($scope,goodsAuditService,toaster){
    goodsAuditService.getShopList($scope).then(function (result) {
        switch (result.status) {
            case 200:
                $scope.shopSelectList = result.data;
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }
    });

    $scope.getList=function(){
        $scope.selectList=[];
        $scope.goods.typeId="";
        if($scope.goods.shopId!=null&&$scope.goods.shopId!=""&&"undefined" != typeof $scope.goods.shopId){
            goodsAuditService.getList($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        $scope.selectList = result.data;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }
            });
        }
    }
}]);

//审核不通过
app.controller('goodsAuditFailCtrl',['$scope','$stateParams','goodsAuditService','toaster','$state', function($scope,$stateParams,goodsAuditService,toaster,$state){
    $scope.data = {
        id:$stateParams.id,
        status:3
    };
    $scope.confirm=function(){
        goodsAuditService.changeStatus($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.goodsAudit');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }
}]);



//批量审核不通过
app.controller('batchGoodsAuditFailCtrl',['$scope','$stateParams','goodsAuditService','toaster','$state', function($scope,$stateParams,goodsAuditService,toaster,$state){
    $scope.data = {
        status:3
    };
    $scope.confirm=function(){
        goodsAuditService.batchUpdateUpStatus($scope).then(function (result) {
            switch (result.status){
                case 200:
                    $scope.types=result.data;
                    $scope.query();
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }
}]);